<template>
  <div>
    <b-alert
      dismissible variant="danger"
      :show="showAlert" @dismissed="showAlert=false"
    >数据加载失败</b-alert>
    <b-alert
      v-if="uploadSuccess" dismissible
      :show="showUpdateAlert" variant="success" @dismissed="showUpdateAlert=false"
    >修改成功</b-alert>
    <b-alert
      v-else dismissible
      :show="showUpdateAlert" variant="danger" @dismissed="showUpdateAlert=false"
    >修改失败</b-alert>
    <b-card class="luke-table-lg">
      <b-row slot="header">
        <h3 class="luke-table-heading">坐庄情况</h3>
        <b-button variant="primary" class="luke-table-refresh" @click="refresh">
          刷新
        </b-button>
      </b-row>
      <b-table
        striped bordered responsive show-empty
        :items="items" :fields="fields"
        class="this-table" dir="rtl"
      >
        <template slot="empty">
          <p class="luke-loading-text" dir="ltr">加载中...</p>
        </template>
        <template slot="col" scope="data">
          {{ words[data.index % 2].first + ' ' + (parseInt(data.index / 2) + 1) + ' ' + words[data.index % 2].second }}
        </template>
        <template v-for="col in columns" :slot="col" scope="data">
          <div dir="ltr" v-bind:key="col">{{ data.item[col] }}</div>
        </template>
        <template slot="lastCol" scope="data">
          <b-row v-if="data.index < 2" dir="ltr">
            <b-form-input
              class="luke-input" v-b-tooltip="'权重'"
              v-model="weights[data.item['col0']]"></b-form-input>
            <b-button
              class="luke-btn" variant="primary"
              v-b-tooltip="'修改'"
              @click="updateWeight(data.item['col0'])"
            >修改</b-button>
          </b-row>
        </template>
      </b-table>
    </b-card>
  </div>
</template>

<style scoped>
  .this-table {
    font-size: 11px;
  }
  .row {
    padding: 10px;
  }
  .luke-input, .luke-btn {
    min-width: 55px;
    flex: 1;
    overflow: hidden;
    font-size: 11px;
  }
  .luke-btn {
    text-align: center;
  }
</style>

<script>
  import Config from '@/assets/global-config'
  import axios from 'axios'
  export default {
    name: 'DealerInfo',
    data () {
      return {
        columns: [],
        fields: {
          lastCol: { label: '修改权重' },
          col19: { label: '在线人数' },
          col18: { label: '拍庄价格' },
          col17: { label: '最大下注金额' },
          col16: { label: '是否连续三天登录' },
          col15: { label: '进入房间次数' },
          col20: { label: '今日参与局数' },
          col14: { label: '今日坐庄次数' },
          col13: { label: '近五局输赢金额' },
          col12: { label: '可提佣总金额' },
          col11: { label: '可提佣人数' },
          col10: { label: '大老板下注总金额' },
          col9: { label: '开庄盘次数' },
          col8: { label: '开散盘次数' },
          col7: { label: '历史坐庄次数' },
          col6: { label: '历史坐庄输赢' },
          col5: { label: '历史大老板输赢' },
          col4: { label: '历史下注输赢金额' },
          col3: { label: '介绍人' },
          col2: { label: '金额' },
          col0: { label: 'ID' },
          col1: { label: '昵称' },
          col: { label: '' }
        },
        items: [],
        words: [
          { first: '第', second: '庄单庄' },
          { first: '第', second: '庄牌庄' }
        ],
        weights: {},
        showAlert: false,
        showUpdateAlert: false,
        uploadSuccess: false
      }
    },
    created () {
      for (let i = 0; i < 21; ++i) {
        this.columns.push(`col${i}`)
      }
      this.fetchData(() => {
        for (let i = 0; i < this.items.length; ++i) {
          this.weights[this.items[i]['col0']] = this.items[i]['weight'] !== undefined ? this.items[i]['weight'] : 0
        }
      })
    },
    methods: {
      fetchData (callback) {
        let originData = this.items
        this.items = []
        var that = this
        axios.post(Config.dealer_info_action())
        .then(function (response) {
          that.showAlert = false
          that.items = response.data
          if (callback) {
            callback()
          }
        })
        .catch(function (error) {
          console.log(error)
          that.showAlert = true
          that.items = originData
        })
      },
      refresh () {
        this.fetchData()
      },
      updateWeight (id) {
        this.json = {
          id: id,
          weight: this.weights[id]
        }
        axios.post(Config.modify_weight_action(), JSON.stringify(this.json))
        .then((response) => {
          console.log(response)
          if (response.status === 200 && response.data.rescode === 1) {
            this.uploadSuccess = true
          } else {
            this.uploadSuccess = false
          }
          this.showUpdateAlert = true
        })
        .catch(function (error) {
          console.log(error)
          this.uploadSuccess = false
          this.showUpdateAlert = true
        })
      }
    }
  }
</script>
